//
//  MBLoginViewController.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/23.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBLoginViewController.h"
#import "MBLoginInputTableViewCell.h"
#import "MBLoginFooterView.h"
#import "AppDelegate.h"
#import "MBTabbarController.h"

@interface MBLoginViewController ()
// 每个VC对应的ViewModel
@property (nonatomic,strong) MBLoginViewModel *viewModel;
@property (nonatomic,weak)MBLoginFooterView* footerView;

@end

@implementation MBLoginViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupLayout];
}

-(void)setupLayout
{
    [super setupLayout];

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.naviView.titleLabel.text = @"登录";
    
    @weakify(self);
    [[self.naviView.backButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(SCREEN_STATUS_BAR_HEIGHT+44, 0, 0, 0));
    }];
    
    MBLoginFooterView* footerView = [[MBLoginFooterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44-SCREEN_STATUS_BAR_HEIGHT-180)];
    self.footerView = footerView;
    self.tableView.tableFooterView = footerView;

    [[footerView.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.viewModel clickLogin];
    }];
    
    [RACObserve(self.viewModel, loginSuccess) subscribeNext:^(id  _Nullable x) {
        if ([x boolValue]) {
            AppDelegate* app = (AppDelegate*)[UIApplication sharedApplication].delegate;
            MBTabbarController* tabbarController = [[MBTabbarController alloc]init];
            app.window.rootViewController = tabbarController;
            [app.window makeKeyWindow];
        }
    }];
}

- (Class)cellClassForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [MBLoginInputTableViewCell class];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MBLoginInputTableViewCell *cell = [[self cellClassForRowAtIndexPath:indexPath] cellForTableView:tableView viewModel:[self.viewModel cellViewModelForRowAtIndexPath:indexPath]];
    cell.selectionStyle = [self.viewModel tableViewCellSelectionStyle];
    @weakify(self);
    [[[cell.inputTextField rac_textSignal]takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self);
        if ([cell.viewModel.dataModel.placeHolder isEqualToString:@"请输入账号"]) {
            self.viewModel.loginParam.username = cell.inputTextField.text;
        }else {
            self.viewModel.loginParam.password = cell.inputTextField.text;
        }
        
        if (self.viewModel.loginParam.username.length >0 && self.viewModel.loginParam.password.length >= 6 && self.viewModel.loginParam.password.length <= 20) {
            self.footerView.isEnable = YES;
        }

    }];
    return cell;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.view endEditing:YES];
}



@end
